package com.vfa.test;

import java.util.ArrayList;

import com.vfa.vocaburay.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class QuestionDetail extends Activity{
	
	ListView listAnswer;
	ArrayList<AnswerE> arraylist;
	QuestionData data;
	AnswerAdapter adapter;
	int questionid;
	String question, rightanswer="";
	
	TextView questiondetail, tvrightanswer;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.questiondetail);
		listAnswer = (ListView)findViewById(R.id.listView_Answer);
		
		questiondetail = (TextView)findViewById(R.id.textView_questiondetail);
		tvrightanswer = (TextView)findViewById(R.id.textView_rightanswer);
		
		arraylist = new ArrayList<AnswerE>();
		data = new QuestionData(this);
		
		ShowData();
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		ShowData();
	}
	
	
	public void ShowData(){
		//get bundle
		Bundle bundle = getIntent().getExtras();
		questionid = bundle.getInt("idquestion");
		question = bundle.getString("question");
		//rightanswer = bundle.getString("rightanswer");
		
		//set data to list answer
		arraylist  = data.getAnswer(questionid);
		adapter = new AnswerAdapter(this, R.layout.answer_item, arraylist);
		listAnswer.setAdapter( adapter);
		
		arrayAnswer(arraylist);
		getRightAnswer(arraylist);
		
		questiondetail.setText(question);
		tvrightanswer.setText(rightanswer);
	}
	public void DeleteQuestion (int id ){
		if(id >0){
			data.DeleteQuestion(id);
			ShowData();
		}
	}
	
	public void getRightAnswer(ArrayList<AnswerE> arraylist){
		//int[] arr = new int[arraylist.size()];
		for (int i = 0; i < arraylist.size(); i++) {
			arraylist.get(i).getRiro();
			if(arraylist.get(i).getRiro()==1){
				rightanswer = arraylist.get(i).getAnswer();
			}
		}
	}

	public String[] arrayAnswer(ArrayList<AnswerE> arraylist)
	{
		String[] arr = new String[arraylist.size()];
		for (int i = 0; i < arraylist.size(); i++) {
			arr[i]=arraylist.get(i).getAnswer();
		}
		
		return null;
	}
	
	public void OnClickCloseQuestionDetail(View view){
		QuestionDetail.this.finish();
	}
	public void OnClickUpdateQuestion(View view){
		//Intent intentupdate = new Intent(getApplicationContext(), QuestionManage.class);
		Intent intentupdate = new Intent(getApplicationContext(), InsertQuestion.class);
		intentupdate.putExtra("idquestion", questionid);
		intentupdate.putExtra("question", question);
		intentupdate.putExtra("ranswer", rightanswer);
		intentupdate.putExtra("flagbundle", 2);
		startActivity(intentupdate);
		QuestionDetail.this.finish();
	}
	public void OnClickDeleteQuestion(View view){
		showDialog(0);
//		DeleteQuestion(questionid);
//		QuestionDetail.this.finish();
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		//return super.onCreateDialog(id);
		   AlertDialog.Builder dialog =new AlertDialog.Builder(this);
		switch (id) {
		
		case 0:
			dialog.setTitle(" Notice! ");
			dialog.setMessage("Do you want to delete this question? ");
			dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					try {
						DeleteQuestion(questionid);
					} catch (Exception e) {
						// TODO: handle exception
					}
					Intent intent = new Intent(getApplicationContext(), ListQuestion.class);
					startActivity(intent);
					QuestionDetail.this.finish();
					
				}
			});
			dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			AlertDialog alert1 = dialog.create();
			alert1.show();
			break;
		}
		return null;
   }//end dialog

}
